package com.example.chachachat7v18;

import java.net.URL;
import java.util.Arrays;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

public class HelloController {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private TextField arrTF;

    @FXML
    private Button clearBtn;

    @FXML
    private Label resLabel;

    @FXML
    private Button solveBtn;

    @FXML
    private Label welcomeText;

    @FXML
    void clearBtnClick(ActionEvent event) {
        arrTF.setText("");
        resLabel.setText("");
    }

    @FXML
    void solveBtnClick(ActionEvent event) {
        int[] arr = Arrays.stream(arrTF.getText().split(",")).mapToInt(Integer::parseInt).toArray();
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            int fact = 1;
            int j = 1;
            // вычисляем факториал числа j
            while (j <= arr[i] && fact <= arr[i]) {
                fact *= j;
                j++;
            }
            // если факториал числа j-1 равен arr[i], то добавляем его в сумму
            if (fact == arr[i]) {
                sum += arr[i];
            }
        }
        resLabel.setText("Сумма элементов массива, являющихся факториалом какого-либо числа, равна " + sum);
    }

    @FXML
    void initialize() {
        assert arrTF != null : "fx:id=\"arrTF\" was not injected: check your FXML file 'hello-view.fxml'.";
        assert clearBtn != null : "fx:id=\"clearBtn\" was not injected: check your FXML file 'hello-view.fxml'.";
        assert resLabel != null : "fx:id=\"resLabel\" was not injected: check your FXML file 'hello-view.fxml'.";
        assert solveBtn != null : "fx:id=\"solveBtn\" was not injected: check your FXML file 'hello-view.fxml'.";
        assert welcomeText != null : "fx:id=\"welcomeText\" was not injected: check your FXML file 'hello-view.fxml'.";

    }

}
